﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using Assesment.Library.DomainObject.Administration.UserManagement;
using Assesment.Web.Support;
using Assesment.Web;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using Training.Library.DataAccess.Administration.Catalog;
using Training.Library.DomainObject.Administration.Catalog;
using Assesment.Library.DataAccess.Transaction;
using Assesment.Library.DomainObject.Transaction;

namespace Assesment.Web.Forms.Transaction
{
    public partial class AssesmentPositionIDPForm : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {

            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {
                try
                {
                    

                    KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                    //List<Karyawan> listKaryawan = KaryawanDal.GetList();
                    //var queryKaryawan = listKaryawan.OrderBy(x => x.NamaKaryawan);
                    //listKaryawan = queryKaryawan.ToList();
                    //ddlNIK.Items.Clear();
                    //listKaryawan.ForEach(delegate(Karyawan objKaryawan)
                    //{
                    //    ddlNIK.Items.Add(new ListItem(objKaryawan.NIK+" - " + objKaryawan.NamaKaryawan, objKaryawan.NIK.ToString()));
                    //});

                    //ddlNIK.Items.Insert(0, new ListItem("-- pilih NIK --", string.Empty));

                    AssesmentPositionRepo AssesmentPositionDal = new AssesmentPositionRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                    List<AssesmentPosition> listAssesment = AssesmentPositionDal.GetList();
                    ddlNIK.Items.Clear();
                    listAssesment.ForEach(delegate(AssesmentPosition objAssesmentPosition)
                    {
                        Karyawan objKaryawan = KaryawanDal.GetById(objAssesmentPosition.NIK);
                        string NamaKaryawan = objKaryawan==null?string.Empty:objKaryawan.NamaKaryawan;
                       
                        ddlNIK.Items.Add(new ListItem(objAssesmentPosition.NIK +" - " + NamaKaryawan , objAssesmentPosition.AssesmentPositionID.ToString()));
                    });

                    ddlNIK.Items.Insert(0, new ListItem("-- pilih NIK --", string.Empty));

                    if (id > 0) // Edit an existing item
                    {

                        hfID.Value = id.ToString();

                        AssesmentPositionIDPRepo AssesmentPositionIDPDal = new AssesmentPositionIDPRepo(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);
                        AssesmentPositionIDP obj = AssesmentPositionIDPDal.GetById(id);
                        if (obj != null)
                        {
                            AssesmentPosition objAssesmentPosition = AssesmentPositionDal.GetById(Convert.ToInt32(obj.AssesmentPositionID));
                            if (objAssesmentPosition != null)
                            {
                                ddlNIK.Items.FindByValue(objAssesmentPosition.AssesmentPositionID.ToString()).Selected = true;

                                //KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                                PositionRepo PositionDal = new PositionRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                                Karyawan objKaryawan = KaryawanDal.GetById(objAssesmentPosition.NIK.ToString());
                                if (objKaryawan != null)
                                {
                                    txtNama.Text = objKaryawan.NamaKaryawan;

                                    
                                    Position objPosition = PositionDal.GetById(Convert.ToInt32(objKaryawan.PositionID));
                                    if (objPosition != null)
                                        txtJabatan.Text = objPosition.PositionName;

                                    CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                                    Cabang objCabang = CabangDal.GetById(Convert.ToInt32(objKaryawan.CabangID));
                                    if (objCabang != null)
                                        txtLokasiKerja.Text = objCabang.NamaCabang;
                                }

                                txtTglAssesment.Text = objAssesmentPosition.AssesmentDate.Value.ToString("dd/MM/yyyy");

                                
                                Position objPositionAssesment = PositionDal.GetById(Convert.ToInt32(objAssesmentPosition.PositionIDAssesment));
                                if (objPositionAssesment != null)
                                    txtJabatanTujuan.Text = objPositionAssesment.PositionName;

                                DepartmentRepo DepartmentDal = new DepartmentRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                                Department objDeptAssesment = DepartmentDal.GetById(Convert.ToInt32(objAssesmentPosition.DepartmentIDAssesment));
                                if (objDeptAssesment != null)
                                    txtDepartemen.Text = objDeptAssesment.DepartmentDesc;

                                LocationRepo LocationDal = new LocationRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                                Location objLocation = LocationDal.GetById(Convert.ToInt32(objPositionAssesment.LocationID));
                                if (objPositionAssesment != null)
                                    txtLokasi.Text = objLocation.Name;

                            }

                            //ddlNIK.Items.FindByValue(obj.NIK.ToString()).Selected = true;

                            //KaryawanRepo KaryawanDal = new KaryawanRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                            //Karyawan objKaryawan = KaryawanDal.GetById(obj.NIK);
                            //if (objKaryawan != null)
                            //    txtNama.Text = objKaryawan.NamaKaryawan;


                            //PositionRepo PositionDal = new PositionRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                            //Position objPosition = PositionDal.GetById(Convert.ToInt32(objKaryawan.PositionID));
                            //if (objPosition != null)
                            //{
                            //    txtJabatan.Text = objPosition.PositionName;
                            //    ddlLokasi.Items.FindByValue(objPosition.LocationID.ToString()).Selected = true;

                            //}
                            //Position objPositionAssesment = PositionDal.GetById(Convert.ToInt32(obj.PositionIDAssesment));
                            //if (objPositionAssesment != null)
                            //    ddlLokasi.Items.FindByValue(objPositionAssesment.LocationID.ToString()).Selected = true;

                            ////LocationRepo LocationDal = new LocationRepo(ConfigurationManager.ConnectionStrings["JobDescConn"].ConnectionString);
                            //Location objLocation = LocationDal.GetById(Convert.ToInt32(objPosition.LocationID));
                            //if (objLocation != null)
                            //    txtLokasiKerja.Text = objLocation.Name;

                            //CabangRepo CabangDal = new CabangRepo(ConfigurationManager.ConnectionStrings["TrainingConn"].ConnectionString);
                            //Cabang objCabang = CabangDal.GetById(Convert.ToInt32(objKaryawan.CabangID));
                            //if (objCabang != null)
                            //    txtLokasiKerja.Text = objCabang.NamaCabang;

                            //txtTglAssesment.Text = obj.AssesmentDate.Value.ToString("dd/MM/yyyy");
                            //ddlDepartemen.Items.FindByValue(obj.DepartmentIDAssesment.ToString()).Selected = true;

                            //List<Position> listPosition = PositionDal.GetList();
                            //var queryPosition = listPosition.Where(x => x.DepartmentID == obj.DepartmentIDAssesment);
                            //listPosition = queryPosition.ToList();
                            //ddlJabatanTujuan.Items.Clear();
                            //listPosition.ForEach(delegate(Position objData)
                            //{
                            //    ddlJabatanTujuan.Items.Add(new ListItem(objData.PositionName, objData.PositionID.ToString()));
                            //});

                            //ddlJabatanTujuan.Items.Insert(0, new ListItem("-- pilih jabatan --", string.Empty));

                            //ddlJabatanTujuan.Items.FindByValue(obj.PositionIDAssesment.ToString()).Selected = true;
                            

                        }
                        
                    }

                    GetPrivilege();
                }
                catch (Exception ex)
                {
                    ClientScript.RegisterClientScriptBlock(typeof(Page), "error", "alert('" + " Error :" + ex.Message + "');", true);
                    return;
                }


            }


        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["AssesmentConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }
    }
}